package com.yiqi.lottery.feature.football.view;

import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.view.MenuItem;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.suozhang.framework.framework.BaseActivity;
import com.yiqi.lottery.R;
import com.yiqi.lottery.common.util.DataManager;
import com.yiqi.lottery.entity.bo.BallMatchBo;
import com.yiqi.lottery.feature.football.adapter.FootballAllTypeAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class FootballAllTypeActivity extends BaseActivity implements Toolbar.OnMenuItemClickListener {

    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.list_view)
    ExpandableListView list_view;
    @BindView(R.id.tv_game)
    TextView tv_game;

    List<BallMatchBo.MatchInfo> mMatchInfos;
    int mCurrentType;


    FootballAllTypeAdapter mAdapter;

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        return false;
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_football_match_all_type;
    }

    @Override
    protected void initView() {
        mCurrentType=getIntent().getIntExtra("current_type",6);
        mMatchInfos= (List<BallMatchBo.MatchInfo>) DataManager.getInstance().getTempObject();
        tv_game.setText(Html.fromHtml(mMatchInfos.get(0).getPlay().getHn()+"  VS  "+mMatchInfos.get(0).getPlay().getAn()));
        DataManager.getInstance().setTempObject(null);
        initToolBar(mToolbar, "全部玩法", true, true);
        initExpandableListView();
    }

    private void initExpandableListView() {
        list_view.setGroupIndicator(null);
        list_view.setAdapter(getAdapter());
        boolean[] isCheck;
        int length;
        for(BallMatchBo.MatchInfo matchInfo:mMatchInfos){
            isCheck=matchInfo.getPlay().getIsCheck();
            if(isCheck==null){
                continue;
            }
            length=isCheck.length;
            for(int i=0;i<length;++i) {
                matchInfo.getPlay().setTempCheck(i,isCheck[i]);
            }
        }
        getAdapter().setMatchInfo(mMatchInfos,mCurrentType);
        for (int i = 0; i < getAdapter().getGroupCount(); ++i) {
            list_view.expandGroup(i);
        }
    }

    private FootballAllTypeAdapter getAdapter() {
        if (mAdapter == null) {
            mAdapter = new FootballAllTypeAdapter(this);
        }
        return mAdapter;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.tv_cancel, R.id.tv_ok})
    public void onViewClicked(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.tv_cancel:
                finish();
                break;
            case R.id.tv_ok:
                BallMatchBo.MatchInfo play;
                boolean[] isTempCheck;
                int length;
                for(BallMatchBo.MatchInfo matchInfo:mMatchInfos) {
                    play=matchInfo.getPlay();
                    isTempCheck=play.getIsTempCheck();
                    if(isTempCheck==null){
                        continue;
                    }
                    length=isTempCheck.length;
                    for(int i=0;i<length;++i) {
                        matchInfo.getPlay().setCheck(i,isTempCheck[i]);
                    }
                }
                finish();
                break;
        }
    }


}
